﻿using System;
using System.Collections.Generic;
using System.Windows.Input;
using Agnes.Core;
using System.Windows.Forms;
using System.IO;
using DevExpress.Xpf.RichEdit;
using DevExpress.XtraRichEdit;
using System.Windows;
using System.Windows.Controls;

namespace Agnes.MyDocsLib
{
    /// <summary>
    /// 导入文章命令
    /// </summary>
    public class ExportCmd
    {
        private static ExportCmd _instance;

        /// <summary>
        /// 返回唯一实例
        /// </summary>
        public static ExportCmd Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new ExportCmd();
                }
                return _instance;
            }
        }

        private RoutedCommand _requery;

        /// <summary>
        /// 是否能执行
        /// </summary>
        public bool CanExcuted { get; set; }

        ExportCmd()
        {
            CanExcuted = true;
            _requery = new RoutedCommand("Export Cmd", typeof(ExportCmd));
        }

        /// <summary>
        /// 路由命令
        /// </summary>
        public static RoutedCommand Requery
        {
            get
            {
                return Instance._requery;
            }
        }


        /// <summary>
        /// 执行代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Excuted(object sender, ExecutedRoutedEventArgs e)
        {
            Window win = new Window();
            TextBlock tb = new TextBlock();
            tb.Text = "导出中......";
            tb.Margin = new Thickness(5, 5, 5, 5);
            win.Content = tb;
            win.WindowStyle = WindowStyle.None;
            win.BorderThickness = new Thickness(1.0);
            win.ShowInTaskbar = false;
            win.SizeToContent = SizeToContent.WidthAndHeight;
            win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            win.Topmost = true;

            try
            {
                FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
                if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
                {
                    win.Show();
                    string selPath = folderBrowserDialog.SelectedPath;
                    IArticleNode node = MyDocsLibManager.Instance.ArticleManager.SelectedNode;
                    if (node is Catalog)
                    {
                        Directory.CreateDirectory(Path.Combine(selPath, (node as Catalog).Title));
                        ExportToDir((Catalog)node, Path.Combine(selPath, (node as Catalog).Title));
                    }
                    else
                    { 
                        foreach (IArticleNode i in MyDocsLibManager.Instance.ArticleManager.Roots)
                        {
                            ExportToDir((Catalog)i, selPath);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                AgnesServiceManager.LogServ.Error("MyDocsLib--导出文档集失败，原因：" + ex.Message);
            }
            finally
            {
                win.Close();
            }
        }

        private void ExportToDir(Catalog catalog, string path)
        {
            try
            {
                foreach (IArticleNode i in catalog.Childs)
                {
                    if (i is Catalog)
                    {
                        Directory.CreateDirectory(Path.Combine(path, (i as Catalog).Title));
                        ExportToDir((Catalog)i, Path.Combine(path, (i as Catalog).Title));
                    }
                    else
                    {
                        Article article = (Article)i;
                        
                        try
                        {    
                            AgnesServiceManager.LogServ.Debug("MyDocsLib--导出文档：" + article.CatalogPath + "\\" + article.Title + " 成功");
                            RichEditControl richEditControl = new RichEditControl();
                            richEditControl.Document.LoadDocument(article.DocumentPath, DocumentFormat.Mht);
                            richEditControl.Document.SaveDocument(path + "\\" + article.Title + ".doc", DocumentFormat.Doc);
                        }
                        catch (Exception ex)
                        {
                           // AgnesServiceManager.LogServ.Error("MyDocsLib--导出文档" + article.Title + "失败！" + "原因：" + ex.Message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                AgnesServiceManager.LogServ.Error("MyDocsLib--导出文档失败！" + "原因：" + ex.Message);
            }
        }

        /// <summary>
        /// 判断是否能执行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void CanExcute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (AgnesServiceManager.AgnesSlnService.CurrentSln == null)
            {
                e.CanExecute = false;
            }
            else
            {
                e.CanExecute = true;
            }
        }
    }
}
